<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<title>Simulation</title>
<link rel="stylesheet" type="text/css" href="../style.css">
</head>

<body>

<div align="center">
<table class=allEncompassingTable >
 <tr>
  <td >
<p><a href="../index.html" TARGET="_top"><img src="images/homeImg.png"></a></p>



<h1>Simulation</h1>


<p>A simulation in CoppeliaSim can be started, paused and stopped with [Menu bar --&gt; Simulation --&gt; Start/Pause/Stop simulation] or through the related toolbar buttons:<br>
</p>

<p align=center><img src="images/simulation1.jpg"></p>
<p class=imageLabel>[Simulation start/pause/stop toolbar buttons]</p>

<p>Internally, the simulator will use additional intermediate states in order to correctly inform <a href="scripts.htm">scripts</a> or programs about what will happen next. Following state diagram illustrates the simulator's internal states:<br>
</p>

<p align=center><img src="images/simulation2.jpg"></p>
<p class=imageLabel>[Simulation state diagram]</p>

<p>Scripts and programs should alwaysreact according to the current system call function and possibly the <a href="regularApi/simGetSimulationState.htm">simulation state</a> in order to behave correctly. It is good practice to divide each control code into at least 4 system call functions (e.g. for <a href="childScripts.htm#nonThreaded">non-threaded child scripts</a>):<br>
</p>

<li><strong>Initialization function</strong>:<strong> <strong>sysCall_init</strong></strong>: the function is called only when the script is initialized.</li>

<li><strong>Actuation function</strong>:<strong> <strong>sysCall_actuation</strong></strong>: the function is called when actuation should happen.</li>

<li><strong>Sensing function</strong>:<strong> <strong>sysCall_sensing</strong></strong>: this function is called when sensing should happen.</li>

<li><strong>Clean-up function</strong>:<strong> <strong>sysCall_cleanup</strong></strong>: the function is called just before the script is de-initialized (e.g. at simulation end, or when the script is destroyed).<br>
</li>

<p>For examples on how to arrange a typical script, refer to the <a href="mainScript.htm">main script</a>, the <a href="childScripts.htm">child scripts</a> and <a href="customizationScripts.htm">customization scripts</a> pages.</p>

<br>
<br>
<table class=subsectionTable><tr class=subsectionTd><td class=subsectionTd>
<a name="simulationLoop"></a>Simulation loop
</td></tr></table> 


<p>The simulator operates by advancing the simulation time at constant time steps. Following figure illustrates the main simulation loop:<br>
</p>

<p align=center><img src="images/simulation3.jpg"></p>
<p class=imageLabel>[Main simulation loop]</p>
<br>

<p>
Real-time simulation is supported by trying to keep the simulation time synchronized with the real time:
</p>

<p align=center><img src="images/simulation4.jpg"></p>
<p class=imageLabel>[Real-time simulation loop]</p>
<br>

<p>Following represents a very simplified <a href="mainClientApplication.htm">main client application</a> (messaging, <a href="plugins.htm">plugin</a> handling and other details have been omitted for clarity purpose):<br>
</p>

<pre class=lightBlueBox>
void initializationCallback<br>{<br>    // do some initialization here
}

void loopCallback<br>{<br>    if ( (simGetSimulationState()&amp;sim_simulation_advancing)!=0 )<br>    {<br>        if ( (simGetRealTimeSimulation()!=1)||(simIsRealTimeSimulationStepNeeded()==1) )<br>        {<br>            if ((simHandleMainScript()&amp;sim_script_main_script_not_called)==0)<br>                simAdvanceSimulationByOneStep();<br>        }<br>    }<br>}

void deinitializationCallback<br>{<br>    // do some clean-up here
}
</pre>

<p>Depending on the simulation complexity, performance of the computer and <a href="simulationPropertiesDialog.htm">simulation settings</a>, real-time simulation might not always be possible.<br>
</p>

<br>
<table class=subsectionTable><tr class=subsectionTd><td class=subsectionTd>
<a name="simulationSpeed"></a>Simulation speed
</td></tr></table> 


<p>In non real-time simulations, the simulation speed (i.e. the perceived speed) is mainly dependent on two factors: the simulation time step and the number of simulation passes for one rendering pass (see the <a href="simulationPropertiesDialog.htm">simulation dialog</a> for more details). In the case of a real-time simulation, the simulation speed mainly depends on the real-time multiplication coefficient, but also to a certain degree of the simulation time step (a too small simulation time step might not be compatible with the real-time character of a simulation because of the limited calculation power of the computer). During simulation, the simulation speed can be adjusted with following toolbar buttons:<br>
</p>

<p align=center><img src="images/simulation5.jpg"></p>
<p class=imageLabel>[Simulation speed adjustment toolbar buttons]</p>

<p>The simulation speed is adjusted in a way so that the initial simulation time step is never increased (because this might have as consequence the breaking of a mechanism for example). Following two figures illustrate the simulation speed adjustment mechanisms:<br>
</p>

<p align=center><img src="images/simulation6.jpg"></p>
<p class=imageLabel>[Simulation speed adjustment mechanism for <strong>non real-time simulations</strong>]</p>
<br>

<p align=center><img src="images/simulation7.jpg"></p>
<p class=imageLabel>[Simulation speed adjustment mechanism for <strong>real-time simulations</strong>]</p>

<p>By default, each simulation cycle is composed by following <strong>sequential</strong> operations:</p>
<li>Executing the <a href="mainScript.htm">main script</a></li>
<li>Rendering the scene</li>

<br>
<br>



<br>
<br>
<h3 class=recommendedTopics>Recommended topics</h3>
<li><a href="apiFunctionListCategory.htm#SimulationFunctionality">Related API functions</a></li>
<li><a href="simulationPropertiesDialog.htm">Simulation settings dialog</a></li>
<li><a href="scripts.htm">Scripts</a></li>
<li><a href="mainClientApplication.htm">The main client application</a></li>
<li><a href="plugins.htm">Plugins</a></li>
<br>
<br>

 </tr>
</table> 
</div>  
  
  
</body>

</html>
